/**
 * Created by 97203 on 2018/06/08.
 */
$(function () {
    var dept_datagrid = $("#dept_datagrid");
    var dept_dialog = $("#dept_dialog");
    var dept_form = $("#dept_form");
    var item_dialog = $("#item_dialog");
    var item_form = $("#item_form");
    dept_datagrid.datagrid({
        url: '/claim/query.do',
        fitColumns: true,
        fit: true,
        pagination: true,    //分页条
        rownumbers: true,     //行号列
        singleSelect: true,
        toolbar: '#tb',
        columns: [[
            {field: 'sn', title: '理赔单编号', width: 100},
            {field: 'reporter', title: '报案人', width: 100},
            {field: 'tel', title: '电话', width: 100},
            {field: 'staging', title: '保单编号', width: 100},
            /*{
                field: 'reporter1', title: '联系电话', width: 100, formatter: function (value,row) {
                    return row.reporter ? row.reporter.tel : "";
            }},*/
            {field: 'inputTime', title: '录入时间', width: 100},

            {
                field: 'inputUser', title: '录入人', width: 100, formatter: function (value) {
                return value ? value.username : "";
            }},
            {
                field: 'accidentAudit1', title: '审核时间', width: 100, formatter: function (value,row) {
                return row.accidentAudit ? row.accidentAudit.auditTime : "";
            }},
            {
                field: 'auditUser', title: '审核人', width: 100, formatter: function (value) {
                return value ? value.username : "";
            }},
            {
                field: 'accidentAudit2', title: '审核状态', width: 100, formatter: function (value,row) {
                return row.accidentAudit.state ? "<font color='green'>已审核</font>" : "<font color='red'>未审核</font>";
            }
            }
        ]]
    })
    dept_dialog.dialog({
        width: 350,
        height: 300,
        buttons: '#btn',
        closed: true,
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            dept_form.form('clear');
        }
    })
    item_dialog.dialog({
        width: 500,
        height: 400,
        closed: true,
        title:'数据明细',
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            item_form.form('clear');
        }
    })

    var methodObj = {
        add: function () {
            //设置标题
            dept_dialog.dialog('setTitle', '新增报案单');
            //打开弹出框
            dept_dialog.dialog('open');
        },
        itemView: function () {
            var row = dept_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要编辑的数据！', 'warning');
                return;
            }

            $.get('/claim/getCliamItemByClaimId.do', {claimId: row.id}, function (data) {
                if(data.employee){
                    row["employee.username"] = data.employee.username;
                }
                if (data.state){
                    row["state"] = "已处理"
                }else{
                    row["state"] = "未处理"
                }
                if (data.duty){
                    row["duty"] = "赔"
                }else{
                    row["duty"] = "不赔"
                }
                item_form.form('load', data);
                item_form.form('load', row);
                item_dialog.dialog('open');
            })


        },
        edit: function () {

            var row = dept_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要编辑的数据！', 'warning');
                return;
            }
            $.get('/claim/getCliamItemByClaimId.do', {claimId: row.id}, function (data) {
                row["item.site"]=data.site;
                row["item.carnumber"]=data.carnumber;

                //把数据回显到表单中
                dept_form.form('load', row);
                //设置标题
                dept_dialog.dialog('setTitle', '编辑理赔单号');
                //打开弹出框
                dept_dialog.dialog('open');
            })


        },
        del: function () {
            var row = dept_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您想要删除这条订单吗?', function (r) {
                if (r) {
                    $.get('/claim/delete.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功！', 'info', function () {
                                dept_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },

        reload: function () {
            dept_datagrid.datagrid('reload');
        },
        save: function () {
            //提交ajax
            dept_form.form('submit', {
                url: '/claim/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功！', 'info', function () {
                            //关闭弹出框
                            methodObj.cancel();
                            //在dataGrid重新加载数据
                            dept_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'warning');
                        dept_dialog.dialog('close')
                    }
                }
            })
        },
        cancel: function () {
            dept_dialog.dialog('close');
        },
        query: function () {
            var keyword = $("[name=keyword]").val();
            var beginTime = $("[name=beginTime]").val();
            var endTime = $("[name=endTime]").val();
            dept_datagrid.datagrid('load', {
                keyword: keyword,
                beginTime:beginTime,
                endTime:endTime
            })
        }
    }
    $("a[data-cmd]").click(function () {
        //获取按钮上的data-cmd的值
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })

})
